
$(function(){
	
	$(".nav-search p input").focus(function(){
		$(".nav-search ul").css("display","block");
	}).blur(function(){
		$(".nav-search ul").css("display","none");
	})
	
})

//回到顶部
function Float(elem, tip){
	var that = this;
	this.tip = tip;
	// TOP的点击事件
	tip.click( this.top_click );
	// 当手动拖动滚动条时
	$(window).scroll( function(){
		that.win_scroll();
	} );
}
Float.prototype.top_click = function(){
	$("html,body").animate({"scrollTop":0}, 500);
}
Float.prototype.win_scroll = function(){
	var st = $(document).scrollTop();
	if(0<st){
			this.tip.fadeIn();
		}else{
			this.tip.fadeOut();
		}
}
var f = new Float($("html,body"), $(".rightBar-top"));

$(function(){
	initPro();
})

function initPro() {
	var str = location.href.split("?")[1].split("&");
	var pid = str[0].split("=")[1];
	console.log(pid)
//	var cname = str[1].split("=")[1];
//	var bigArr = [];
	$.ajax({
		type:"GET",
		url:"../json/list.json?id=",
		success:function(d){
			var html1="";
			var html2="";
			var html3="";
			var html4="";
			var html5="";
			var html6="";
			for(var key in d["list1"]){
				if(d["list1"][key]["id"]==pid){
					html1+='<a  href="../index.html">首页 > </a><span class="bread-title">'+d["list1"][key]["title"]+'</span>'
					html2+='<p>'
					for (var i = 0; i < d["list1"][key]["imgs"].length; i++) {
						html2+='<img src='+d["list1"][key]["imgs"][i]+'>'
					}
					
					html2+='</p><ul>'
					for (var i = 0; i < d["list1"][key]["imgs"].length; i++) {
						html2+='<li><img src='+d["list1"][key]["imgs"][i]+'></li>'
					}					
					html2+='</ul>'
					html3+='<h1>'+d["list1"][key]["headline"]+'</h1><p>'+d["list1"][key]["des"]+'</p>';
					html4+='<span>售价</span><div><span class="panel-maney">￥'+d["list1"][key]["price"]+'</span><span class="panel-duration">生产周期：<span>'+d["list1"][key]["time"]+'</span>天</span></div>';
					for (var j = 0; j < d["list1"][key]["color"].length; j++) {
						html5+='<li><img src='+d["list1"][key]["color"][j]+'><em></em><i></i></li>'
					}
					for (var j = 0; j < d["list1"][key]["size"].length; j++) {
						html6+='<li class="specs-detail"><span>'+d["list1"][key]["size"][j]+'</span><em></em></li>'
					}
				}
			}
			$(".section-bread").html(html1);
			$(".editor-picture").html(html2);
			$(".panel-top").html(html3);
			$(".panel-press").html(html4);
			$(".specs-dimension div .col").html(html5);
			$(".specs-dimension div .size").html(html6);
			$(".editor-picture ul li").eq(0).addClass("main-active");
			$(".editor-picture ul").on("click","li",function(){
				$(this).addClass("main-active").siblings().removeClass("main-active");
				var index1 = $(this).index();
				
				$(".editor-picture p img").eq(index1).css("display","block").siblings().css("display","none");
				
			});
			$(".specs-dimension div ul li").eq(0).addClass("lowModel-specs-active");
			$(".specs-dimension div .size li").eq(0).addClass("lowModel-specs-active");
			$(".specs-dimension div ul").on("click","li",function(){
				$(this).addClass("lowModel-specs-active").siblings().removeClass("lowModel-specs-active");
			});
			
			$(".panel-num .panel-minus").click(function(){
				var n = $(".panel-number").html();
				if(n>1){
					n--;
					var n = $(".panel-number").html(n);
				}
			});
			$(".panel-num .panel-add").click(function(){
				var n = $(".panel-number").html();
				n++
				$(".panel-number").html(n)
			})
		}
	});
	
}

//加入购物车

$(function(){
	//加载已有的购物车信息
	loadCart();
	
	//给购物车按钮加一个点击事件
	$(".user-shopCar").click(function(){
		location.href = "shopcar.html";
	})
	//给加入购物车按钮添加点击事件
	$("#addShopcar").click(function(e){
		//获取商品的id（用来区分不同的商品）
		var str = location.href.split("?")[1].split("&");	
		var goodId = str[0].split("=")[1];
		//获取商品的名称
		var goodName = $(".panel-top h1").html();
		//获取商品尺寸
		var goodSize = $(".size li").siblings(".lowModel-specs-active").children("span").html();
		//获取商品的价格
		var pattern = /(\d+)/;
		var goodPrice = parseFloat($(".panel-press .panel-maney").html().substring(1));
		//获取商品数量
		var goodNum = $(".panel-num .panel-number").html();
		//获取商品的图片src
		
		var goodSrc = $(".specs-dimension .col li").siblings(".lowModel-specs-active").children("img").attr("src")?$(".specs-dimension .col li").siblings(".lowModel-specs-active").children("img").attr("src") : $(".specs-dimension .col li img").attr("src");
		
//					document.cookie = "key=value"
		
		//获取cookie中的信息
		//如果cookie中没有信息会返回一个undefined ,我所须是一个字符串类型的数据，所以将它转成一个“”空字符串。保持数据类型一致。
		var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
		//将字符串转成对象
		var cartObj = convertCartStrToObj(cartStr);
		//判断该商品是否已经在购物车中存在
		if(goodId in cartObj){
			//如果已存在，那么该商品的数量加1
			cartObj[goodId].num += 1;
		}else{
			//如果不存在，那么将新商品的信息存入
			cartObj[goodId] = {
				name : goodName,
				price : goodPrice,
				num : goodNum,
				src : goodSrc,
				size: goodSize
			};
		}
		
		//将新的购物车信息存回cookie
		//将对象转为字符串
		
		cartStr = convertObjToCartStr(cartObj);
		//存入cookie
		//document.cookie = "key=value"
		$.cookie("cart",cartStr,{expires : 7,path:"/"});
				
		console.log(cartObj)
			
		
		
	})
});


function convertCartStrToObj(cartStr){
	
	//如果是空字符串，即没有购物车信息，那么购物车为空，直接返回一个空对象
	if(!cartStr){
		return {};
	}
	var goods = cartStr.split(":");
	var obj = {};
	for(var i = 0; i < goods.length; i ++){
		var data = goods[i].split(",");
		//以商品的id为健，商品的其他信息为值，这个值也设计为一个对象
		obj[data[0]] = {
			name : data[1],
			price : parseFloat(data[2]),
			num : parseInt(data[3]),
			src : data[4],
			size : data[5]
		}
	}
	return obj;
}
function convertObjToCartStr(obj){
		
		var cartStr = "";
		//遍历对象
		for(var id in obj){
			if(cartStr){
				cartStr += ":";
			}
			
			cartStr += id + "," + obj[id].name + "," + obj[id].price + "," + obj[id].num + "," + obj[id].src+","+obj[id].size;
		}
		return cartStr;
}

//加载购物车中的信息（使商品页与购物车页中的购物车数量同步）
function loadCart(){
	var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
		var cartObj = convertCartStrToObj(cartStr);
		//获取到购物车中所有商品的数量
		var total = 0;
		for(var id in cartObj){
			total += cartObj[id].num;
		}
		$("#shopCar").html(total);
}


$("#addShopcar").click(function(){
	location.href = "shopcar.html";
})
